<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="简历ID" prop="myResId">
      <el-input v-model="dataForm.myResId" placeholder="简历ID"></el-input>
    </el-form-item>
    <el-form-item label="录入时间" prop="myTime">
      <el-input v-model="dataForm.myTime" placeholder="录入时间"></el-input>
    </el-form-item>
    <el-form-item label="状态 0：预约，1：储备，2：入职，3：无效" prop="myResState">
      <el-input v-model="dataForm.myResState" placeholder="状态 0：预约，1：储备，2：入职，3：无效"></el-input>
    </el-form-item>
    <el-form-item label="录入人ID" prop="myHrId">
      <el-input v-model="dataForm.myHrId" placeholder="录入人ID"></el-input>
    </el-form-item>
    <el-form-item label="来源 1：自己录入，2：朋友分享" prop="myResFrom">
      <el-input v-model="dataForm.myResFrom" placeholder="来源 1：自己录入，2：朋友分享"></el-input>
    </el-form-item>
    <el-form-item label="备注1" prop="myBz1">
      <el-input v-model="dataForm.myBz1" placeholder="备注1"></el-input>
    </el-form-item>
    <el-form-item label="备注2" prop="myBz2">
      <el-input v-model="dataForm.myBz2" placeholder="备注2"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          myId: 0,
          myResId: '',
          myTime: '',
          myResState: '',
          myHrId: '',
          myResFrom: '',
          myBz1: '',
          myBz2: ''
        },
        dataRule: {
          myResId: [
            { required: true, message: '简历ID不能为空', trigger: 'blur' }
          ],
          myTime: [
            { required: true, message: '录入时间不能为空', trigger: 'blur' }
          ],
          myResState: [
            { required: true, message: '状态 0：预约，1：储备，2：入职，3：无效不能为空', trigger: 'blur' }
          ],
          myHrId: [
            { required: true, message: '录入人ID不能为空', trigger: 'blur' }
          ],
          myResFrom: [
            { required: true, message: '来源 1：自己录入，2：朋友分享不能为空', trigger: 'blur' }
          ],
          myBz1: [
            { required: true, message: '备注1不能为空', trigger: 'blur' }
          ],
          myBz2: [
            { required: true, message: '备注2不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.myId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.myId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/myresume/info/${this.dataForm.myId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.myResId = data.myresume.myResId
                this.dataForm.myTime = data.myresume.myTime
                this.dataForm.myResState = data.myresume.myResState
                this.dataForm.myHrId = data.myresume.myHrId
                this.dataForm.myResFrom = data.myresume.myResFrom
                this.dataForm.myBz1 = data.myresume.myBz1
                this.dataForm.myBz2 = data.myresume.myBz2
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/myresume/${!this.dataForm.myId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'myId': this.dataForm.myId || undefined,
                'myResId': this.dataForm.myResId,
                'myTime': this.dataForm.myTime,
                'myResState': this.dataForm.myResState,
                'myHrId': this.dataForm.myHrId,
                'myResFrom': this.dataForm.myResFrom,
                'myBz1': this.dataForm.myBz1,
                'myBz2': this.dataForm.myBz2
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
